package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.LeaveRetireUser;
import com.ruoyi.system.mapper.LeaveRetireUserMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LeaveRetireMapper;
import com.ruoyi.system.domain.LeaveRetire;
import com.ruoyi.system.service.ILeaveRetireService;

/**
 * 离退休申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-21
 */
@Service
public class LeaveRetireServiceImpl implements ILeaveRetireService 
{
    @Autowired
    private LeaveRetireMapper leaveRetireMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private LeaveRetireUserMapper leaveRetireUserMapper;
    /**
     * 查询离退休申请
     * 
     * @param id 离退休申请主键
     * @return 离退休申请
     */
    @Override
    public LeaveRetire selectLeaveRetireById(Long id)
    {
        return leaveRetireMapper.selectLeaveRetireById(id);
    }

    /**
     * 查询离退休申请列表
     * 
     * @param leaveRetire 离退休申请
     * @return 离退休申请
     */
    @Override
    public List<LeaveRetire> selectLeaveRetireList(LeaveRetire leaveRetire)
    {

        if(leaveRetire == null){
            leaveRetire = new LeaveRetire();
        }
        if(SecurityUtils.hasRole("common") || SecurityUtils.hasRole("director")){
            leaveRetire.setUserId(SecurityUtils.getUserId());
            return leaveRetireMapper.selectLeaveRetireList(leaveRetire);
        }


        return leaveRetireMapper.selectLeaveRetireList(leaveRetire);
    }

    /**
     * 新增离退休申请
     * 
     * @param leaveRetire 离退休申请
     * @return 结果
     */
    @Override
    public int insertLeaveRetire(LeaveRetire leaveRetire)
    {
        leaveRetire.setCreateTime(DateUtils.getNowDate());
        leaveRetire.setUserId(SecurityUtils.getUserId());
        leaveRetire.setProgress("1");

        String type = leaveRetire.getType();
            //判断插入的数据是退休还是辞职
            //如果是退休
            //获取用户
            SysUser sysUser = userMapper.selectUserById(SecurityUtils.getUserId());
            //获取用户入职时间
            Date createTime = sysUser.getCreateTime();
            //获取当前时间
            Date endWork = leaveRetire.getEndWork();
            //用当前时间减去入职时间，获取工作多少年多少个月
            // 计算两个日期之间的毫秒数
            long diffInMillies = Math.abs(endWork.getTime() - createTime.getTime());

            // 将毫秒数转换为年和月
            long years = TimeUnit.MILLISECONDS.toDays(diffInMillies) / 365;
            long months = TimeUnit.MILLISECONDS.toDays(diffInMillies) % 365 / 30;

            leaveRetire.setWorkingTime(years+"年"+months+"月");
            leaveRetire.setStartWork(createTime);

        return leaveRetireMapper.insertLeaveRetire(leaveRetire);
    }

    /**
     * 修改离退休申请
     * 
     * @param leaveRetire 离退休申请
     * @return 结果
     */
    @Override
    public int updateLeaveRetire(LeaveRetire leaveRetire)
    {
        //更新离退申请
        //如果前台操作是审核通过退休的话
        if(leaveRetire.getProgress().equals("2") && leaveRetire.getType().equals("0")){
            //如果是申请通过操作
            LeaveRetireUser leaveRetireUser = new LeaveRetireUser();
            leaveRetire.setUserId(leaveRetire.getUserId());
            SysUser sysUser = userMapper.selectUserById(leaveRetire.getUserId());
            leaveRetireUser.setUserId(sysUser.getUserId());
            leaveRetireUser.setDeptId(sysUser.getDeptId());
            leaveRetireUser.setAccount(sysUser.getAccount());
            leaveRetireUser.setUserName(sysUser.getUserName());
            leaveRetireUser.setNickName(sysUser.getNickName());
            leaveRetireUser.setSex(sysUser.getSex());
            leaveRetireUser.setEmail(sysUser.getEmail());
            leaveRetireUser.setTitle(sysUser.getTitle());
            leaveRetireUser.setPhonenumber(sysUser.getPhonenumber());
            leaveRetireUser.setPension(8888.88);
            leaveRetireUserMapper.insertLeaveRetireUser(leaveRetireUser);
        }
//        leaveRetire.setProgress("2");
        leaveRetire.setUpdateTime(DateUtils.getNowDate());
        return leaveRetireMapper.updateLeaveRetire(leaveRetire);
    }

    /**
     * 批量删除离退休申请
     * 
     * @param ids 需要删除的离退休申请主键
     * @return 结果
     */
    @Override
    public int deleteLeaveRetireByIds(Long[] ids)
    {
        return leaveRetireMapper.deleteLeaveRetireByIds(ids);
    }

    /**
     * 删除离退休申请信息
     * 
     * @param id 离退休申请主键
     * @return 结果
     */
    @Override
    public int deleteLeaveRetireById(Long id)
    {
        return leaveRetireMapper.deleteLeaveRetireById(id);
    }
}
